'''
Created on 2010/08/11

ref annexI.4 Formart Flags

@author: yukiyonezawa
'''
from pyrfid.Util.Util import Util
class FormatFlag(object):
    
    __TERMINATION_PATTERN = "00000000"
    
    BASE_IDLPO = "IDLPO"
    BASE_IDMPO = "IDLMO"
    
    N_IDLPO_NON_DEFAULT = "IDLPO_NON_DEFAULT"
    N_IDLPO_DEFAULT = "IDLPO_DEFAULT"
    
    A_IDLPO_ADDENDUM = "IDLPO_ADDENDUM"
    A_IDLPO_NON_ADDENDUM = "IDLPO_NON_ADDENDUM"
    
    __BASE_STRICTURE_TYPE = ""
    __BASE_STRUCTURE_IDLPO = "000010"
    __BASE_STRUCTURE_IDMPO = "000011"
    __BASE_STRUCTURE_EXTERNAL_PATTERN = "000001"
    
    
    __NON_DEFAULT_IDLPO_N = "1"# allow multiple tables
    __DEFAULT_IDLPO_N = "0"
    
    __ADDENDUM_A = "1" #Addendum pointer at end of Object Info 
    __NON_ADDENDUM_A = "0"
    
    __bit = ""
    
    
    def __init__(self, base_structure, option_n, option_a):
        structure_bit = ""
                
        if base_structure == FormatFlag.BASE_IDLPO:
            self.__BASE_STRICTURE_TYPE = FormatFlag.BASE_IDLPO
            structure_bit = self.__BASE_STRUCTURE_IDLPO
        elif base_structure == FormatFlag.BASE_IDMPO:
            self.__BASE_STRICTURE_TYPE = FormatFlag.BASE_IDMPO
            structure_bit = self.__BASE_STRUCTURE_IDMPO
        else:
            structure_bit = self.__BASE_STRUCTURE_EXTERNAL_PATTERN 
        
        if option_n == FormatFlag.N_IDLPO_DEFAULT:    
            structure_bit += self.__NON_ADDENDUM_A
        elif option_n == FormatFlag.N_IDLPO_NON_DEFAULT:
            structure_bit += self.__NON_DEFAULT_IDLPO_N
        
        if option_a == FormatFlag.A_IDLPO_ADDENDUM:
            structure_bit += self.__ADDENDUM_A
        elif option_a == FormatFlag.A_IDLPO_NON_ADDENDUM:
            structure_bit += self.__NON_ADDENDUM_A 
        
        self.__bit = structure_bit
        
    def getBit(self):
        return self.__bit
    
    def getBaseStructureType(self):
        return self.__BASE_STRICTURE_TYPE
    
    @classmethod
    def analyzeFormatFlag(cls, data_hex):        
        data_bit = Util.convertHexToBit(data_hex)
        base_structure = data_bit[0:6]
        option_n = data_bit[6]
        option_a = data_bit[7]
        
        structure_type = ""
        if base_structure == cls.__BASE_STRUCTURE_IDLPO:
            structure_type = FormatFlag.BASE_IDLPO
        elif base_structure == cls.__BASE_STRUCTURE_IDMPO:
            structure_type = FormatFlag.BASE_IDMPO
        else:
            raise "NON IMPLEMENTED"
        
        option_n_type = ""
        if option_n == cls.__DEFAULT_IDLPO_N:
            option_n_type = FormatFlag.N_IDLPO_DEFAULT
        elif option_n == cls.__NON_DEFAULT_IDLPO_N:
            option_n_type = FormatFlag.N_IDLPO_NON_DEFAULT
        else:
            raise "NON IMPLEMENTED"
        
        option_a_type = ""
        if option_a == cls.__ADDENDUM_A:
            option_a_type = FormatFlag.A_IDLPO_ADDENDUM
        elif option_a == cls.__NON_ADDENDUM_A:
            option_a_type = FormatFlag.A_IDLPO_NON_ADDENDUM
        else:
            raise "NON IMPLEMENTED"
        
        print structure_type,"|", option_n_type,"|", option_a_type
        return FormatFlag(structure_type, option_n_type, option_a_type)
        
        
        
        